topdir=../

include $(topdir)makefile.inc

CFLAGS+=$(CTHUMB)

# warning: library order matters!
LDLIBS= -lgcc
LDOPTS=-nostdlib -Wl,--allow-shlib-undefined -Wl,-T,$(topdir)tools/link-boot.ld
LDOPTS+=-Wl,-N,-Ttext,$(MEMISOSTART)

all: main.bin

OPT_OBJS=
ifdef OPT_DEBUGGING
# global in root makefile.inc
#CFLAGS+=-DOPT_DEBUGGING
OPT_OBJS+=gui_debug.o gui_bench.o 
endif
ifdef OPT_MD_DEBUG
CFLAGS+=-DOPT_MD_DEBUG
endif 
ifdef OPT_PTP
CFLAGS+=-DOPT_PTP
OPT_OBJS+=ptp.o 
endif
ifdef OPT_EXMEM_MALLOC
#CFLAGS+=-DOPT_EXMEM_MALLOC
OPT_OBJS+=suba.o
endif

OBJS=entry.o nothumb.o main.o gui_draw.o gui_menu.o gui_palette.o gui_mbox.o \
     gui_fselect.o gui.o kbd.o action_stack.o conf.o \
     histogram.o gui_batt.o gui_osd.o \
     gui_lang.o gui_mpopup.o \
     levent.o bitvector.o console.o $(OPT_OBJS)

gui.o: FORCE

FORCE:

nothumb.o: nothumb.c
	@echo $< \-\> $@
	$(CC) $(CFLAGS) -marm -nostdinc -c -o $@ $<

main.bin: main.elf
	@echo $< \-\> $@
	$(OBJDUMP) -z -d main.elf > main.dump
	$(OBJCOPY) -O binary main.elf main.bin

main.elf: $(OBJS) $(topdir)platform/$(PLATFORM)/libplatform.a \
          $(topdir)platform/$(PLATFORM)/sub/$(PLATFORMSUB)/libplatformsub.a \
          $(topdir)lib/font/libfont.a $(topdir)lib/math/libmath.a \
          $(topdir)lib/lang/liblang.a \
          $(topdir)lib/armutil/libarmutil.a
	@echo \-\> $@
	$(CC) $(CFLAGS) -o $@ --start-group $^  $(LDLIBS) --end-group $(LDFLAGS) $(LDOPTS)
	( $(NM) $@ | grep ' U ' > $@.syms ) && echo "error: unresolved symbols in $@ (see $@.syms)" && exit 1 || exit 0

	$(SIZE) $@ | tee $(topdir)size.txt
	@printf " %s-%s (%s, #%s): MEMISOSIZE used: 0x%s\n" \
   $(PLATFORM) $(PLATFORMSUB) $(PLATFORMOS) $(PLATFORMID) \
   `tail -c16 $(topdir)size.txt | head -c6` >> $(topdir)bin/caminfo.txt
	rm -f $(topdir)size.txt

clean:
	rm -f $(OBJS) main.bin main.elf main.dump main.elf.syms

distclean: clean
	rm -f $(OBJS:.o=.d)

include $(topdir)bottom.inc
